package com.yumeno.epidemic.Controller;

import com.yumeno.epidemic.beans.*;
import com.yumeno.epidemic.service.EpidemicService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/epidemicData")
public class EpidemicController {

    private Logger logger = Logger.getLogger(EpidemicController.class);

    @Autowired
    private EpidemicService epidemicService;

    //录入新增数据
    @PostMapping("/ajax/input")
    @ResponseBody
    public AjaxRespInfo inputData(@RequestBody DailyEpidemicInfo dailyEpidemicInfo, Model model, HttpSession session) {
        //获取当前的用户id
        UserInfo user = (UserInfo) session.getAttribute("userLogined");
        AjaxRespInfo<List<ProvinceInfo>> ajaxRespInfo = new AjaxRespInfo<>();
        //判断用户是否登录
        if (user == null) {
            ajaxRespInfo.setCode(-2);
            ajaxRespInfo.setMsg("您还未登录！");
        } else {
            //保存数据并将返回的下一批省份
            List<ProvinceInfo> list = null;
            list = this.epidemicService.saveData(dailyEpidemicInfo, user.getUserId());

            ajaxRespInfo.setData(list);
        }
        return ajaxRespInfo;
    }

    //获取最新疫情数据
    @GetMapping("/ajax/latestData")
    @ResponseBody
    public AjaxRespInfo latestData() {
        AjaxRespInfo<List<EpidemicDetailInfo>> ajaxRespInfo = new AjaxRespInfo<>();
        List<EpidemicDetailInfo> list = epidemicService.findLatestData();
        ajaxRespInfo.setData(list);
        return ajaxRespInfo;
    }

}
